<?php

namespace app\models;

use Yii;
use app\components\Fun;
use app\components\API;
/**
 * This is the model class for table "{{%config}}".
 *
 * @property string $id
 * @property string $val
 * @property string $type
 */
class Config extends \yii\db\ActiveRecord
{
    public $quanju;
    
    /**
     * 记录BaseForm对象
    */
   public  $BaseForm=null;


   /**
     * 获取指定的值
    */
    public function get_val($key,$type){
         
      //   $model=  Config::find()->where(['id'=>$key,'type'=>  $type])->select('val')->one();
        $model=  Config::find()->where('id LIKE :id AND type LIKE :type',[':id'=>$key,':type'=>  $type])->select('val')->one();
         if($model===null){
             return '';
         }
         return $model->val;
    }
    
    
       /**
     * 设定指定字段的值
    */
    public function set_val($key,$type,$val){
       $model=  Config::find()->where('id LIKE :id AND type LIKE :type',[':id'=>$key,':type'=>  $type])->one();
       
       $Labels=$this->BaseForm->attributeLabels();
       $title='';
       if($Labels[$key]){
           $title=$Labels[$key];
       }
         if($model===null){ 
              $model=new Config;
              $model->id=$key;
              $model->type=$type;
             $model->val=$val;
             $r=$model->save();
             Log::addLogUpBaseForm($this->BaseForm->className(),$title.'{'.$type.'.'.$key.'}', $model->val,$val,$r);
             if(!$r){
                 Fun::showmsg(API::tostr($model->getErrors()));
             }
             return ;
         }
         $Mval=$model->val;
         if($model->val!==$val){
            $model->val=$val;
            $up=$model->update();
            Log::addLogUpBaseForm($this->BaseForm->className(),$title.'{'.$type.'.'.$key.'}', $Mval,$val,$up);
             
            if($up!='1'){
                Fun::showmsg('更新数据错误'.$type.'.'.$key);
            }
         }
    }
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%config}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        //    [['id', 'val'], 'required'],
           // [['val'], 'string'],
          //  [['id', 'type'], 'string', 'max' => 64]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'val' => 'Val',
            'type' => '类名',
        ];
    }
}
